package dsk.demo.product.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import dsk.demo.common.utils.PageUtils;
import dsk.demo.common.utils.Query;
import dsk.demo.product.dao.ProductRefDao;
import dsk.demo.product.entity.ProductRefEntity;
import dsk.demo.product.service.ProductRefService;
import dsk.demo.product.util.StrUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.*;
import java.util.stream.Collectors;


@Service("productRefService")
public class ProductRefServiceImpl extends ServiceImpl<ProductRefDao, ProductRefEntity> implements ProductRefService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ProductRefEntity> page = this.page(
                new Query<ProductRefEntity>().getPage(params),
                new QueryWrapper<ProductRefEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 导入同一个品牌 的 不同OEM号码,支持oem号码 字段使用换行符
     *
     * @param file
     * @return
     */
    @Override
    public List<ProductRefEntity> excel2ProductEntityList(MultipartFile file) {
        try {
            InputStream inputStream = file.getInputStream();
            ExcelReader reader = ExcelUtil.getReader(inputStream);
            Map<String, String> map = new HashMap<>();
            map.put("本厂编号", "productNo");
            map.put("品牌", "brand");
            map.put("OEM号码", "refNo");
            reader.setHeaderAlias(map);
            List<ProductRefEntity> all = reader.readAll(ProductRefEntity.class);
            //将分行的oem号码 处理为不同对象.
            List<ProductRefEntity> finalList = new ArrayList<>();
            all.stream().forEach(item -> {
                if (StrUtil.isBlank(item.getRefNo()) || StrUtil.isBlank(item.getProductNo())) {
                    return;
                }
                String[] split = item.getRefNo().split("\\n");
                if (split.length >= 1) {
                    for (String refNo : split) {
                        if (StrUtil.isBlank(refNo)) {
                            continue;
                        }
                        ProductRefEntity productRefEntity = new ProductRefEntity();
                        productRefEntity.setProductNo(item.getProductNo());
                        String[] split1 = refNo.trim().split(":");
                        if (split1.length > 1) {
                            productRefEntity.setBrand(split1[0].trim());
                            productRefEntity.setRefNo(split1[1].trim());
                        } else {
                            productRefEntity.setBrand(item.getBrand());
                            productRefEntity.setRefNo(refNo.trim());
                        }
                        finalList.add(productRefEntity);
                    }
                } else {
                    finalList.add(item);
                }
            });
            return finalList;
        } catch (Exception e) {
            System.out.println(e);
            return null;
        }
    }

    @Override
    public void insertBatch(Set<ProductRefEntity> collect) {
        if (CollectionUtil.isNotEmpty(collect)) {
            //补齐查询号码
            Set<ProductRefEntity> collect1 = collect.stream().filter(item -> StrUtil.isNotBlank(item.getRefNo())).collect(Collectors.toSet());
            for (ProductRefEntity entity : collect1) {
                entity.setSearchProductNo(StrUtil.getFoundString(entity.getProductNo().trim()));
                entity.setSearchNo(StrUtil.getFoundString(entity.getRefNo()));
                entity.setProductNo(entity.getProductNo().trim());
                entity.setRefNo(entity.getRefNo().trim());
            }
            this.getBaseMapper().insertBatch(collect1);
        }
    }

    @Override
    public boolean saveOrUpdateBatch(Collection<ProductRefEntity> entityList) {
        if (CollectionUtil.isNotEmpty(entityList)) {
            //补齐查询号码
            Set<ProductRefEntity> collect1 = entityList.stream().filter(item -> StrUtil.isNotBlank(item.getRefNo())).collect(Collectors.toSet());
            for (ProductRefEntity entity : collect1) {
                entity.setSearchProductNo(StrUtil.getFoundString(entity.getProductNo().trim()));
                entity.setSearchNo(StrUtil.getFoundString(entity.getRefNo()));
                entity.setProductNo(entity.getProductNo().trim());
                entity.setRefNo(entity.getRefNo().trim());
            }
            return super.saveOrUpdateBatch(collect1);
        }
        return true;
    }

    @Override
    @Transactional
    public int updateRefProductNo(String oldNo, String newNo) {
        ProductRefEntity entity = new ProductRefEntity();
        entity.setProductNo(newNo);
        UpdateWrapper<ProductRefEntity> wrapper = new UpdateWrapper<ProductRefEntity>();
        wrapper.set("product_no", newNo).set("search_product_no", StrUtil.getFoundString(newNo))
                .eq("product_no", oldNo);
        return this.getBaseMapper().update(entity, wrapper);
    }

}
